class Solution(object):
    def largestPalindrome(self, n):
        """
        :type n: int
        :rtype: int
        """
        if n == 1:
            return 9
        p = 10 ** n - 1
        while True:
            t = int(str(p) + str(p)[::-1])
            x = 10 ** n - 1
            while x >= t ** 0.5:
                if t % x == 0 and len(str(t // x)) == n:
                    return t % 1337
                x -= 1
            p -= 1



data = Solution()
n = 8
print(data.largestPalindrome(n))